//  
//  RequestBroker.cs
//  
//  Author:
//       Christian Winkel <alphawolf1988@gmail.com>
// 
//  Copyright (c) 2012 
// 
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
// 
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Net;
using Jayrock.JsonRpc;
using Jayrock.Json;
using Jayrock.Json.Conversion;

namespace Lypshare.Core
{
	/// <summary>
	/// Request broker.
	/// </summary>
	public class RequestBroker
	{
		// --------------------------------------------------------
        // Static Functions
        // --------------------------------------------------------
		
		
		/// <summary>
		/// Builds the request object.
		/// </summary>
		/// <returns>
		/// The request object.
		/// </returns>
		/// <param name='method'>
		/// Method.
		/// </param>
		/// <param name='parameters'>
		/// Parameters.
		/// </param>
		private static string BuildRequestObject(string method, String parameters)
		{
			var writer = new JsonTextWriter();
			
			writer.WriteStartObject();
			writer.WriteMember("id");
			writer.WriteNumber(0);
			writer.WriteMember("method");
			writer.WriteString( method);
			writer.WriteMember( "params" );
			writer.WriteString(parameters);
			writer.WriteEndObject();
			
			return writer.ToString();
			
		} // BuildRequestObject
		
		
		// --------------------------------------------------------
        // Class Variables
        // --------------------------------------------------------
		
		
		/// <summary>
		/// The _listener.
		/// </summary>
		private HttpListener _listener = null;
		
		
		/// <summary>
		/// The _service.
		/// </summary>
		private JsonRpcService _service = null;
		
		
		// --------------------------------------------------------
        // Class Properties
        // --------------------------------------------------------
		
		
		/// <summary>
		/// Gets a value indicating whether this instance is listening.
		/// </summary>
		/// <value>
		/// <c>true</c> if this instance is listening; otherwise, <c>false</c>.
		/// </value>
		public bool IsListening
		{
			get { return _listener.IsListening; }
			
		} // Property: IsListening
		
		
		// --------------------------------------------------------
        // Class Functions
        // --------------------------------------------------------
		
		
		/// <summary>
		/// Initializes a new instance of the <see cref="Lypshare.Core.RequestBroker"/> class.
		/// </summary>
		public RequestBroker(JsonRpcService service)
		{
			//if(service == null)
			//	throw new ArgumentNullException("service");
	
			_listener = new HttpListener();		
			_service = service;
			
		} // Constructor
		
		
		/// <summary>
		/// Run the specified uriPrefixes.
		/// </summary>
		/// <param name='uriPrefixes'>
		/// URI prefixes.
		/// </param>
		public void Run(params String[] uriPrefixes)
		{
			if(IsListening) 
				throw new InvalidOperationException(
					"Request broker already listening.");
			
			foreach(var uriPrefix in uriPrefixes)
				_listener.Prefixes.Add(uriPrefix);
			
			_listener.Start();
			
			_listener.BeginGetContext(
				GetContextCallback, null);
			
		} // Run
		
		//http://localhost:50000/funcname?param1=bla
		
		/// <summary>
		/// Gets the context callback.
		/// </summary>
		/// <param name='result'>
		/// Result.
		/// </param>
		private void GetContextCallback(IAsyncResult result)
		{
			var context = _listener
				.EndGetContext(result);
			
		    
			
			if(IsListening) 
				_listener.BeginGetContext(
					GetContextCallback, null);
			
		} // GetContextCallback
		
		
		/// <summary>
		/// Shutdown this instance.
		/// </summary>
		public void Shutdown()
		{
			if(!IsListening) return;
			
			_listener.Stop();
			_listener.Prefixes.Clear();
			
		} // Shutdown
		
	} // class RequestBroker
} // namespace Lypshare.Core